package org.example.meitiguanli.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.example.core.constanct.CommonUtils;
import org.example.core.mysql.SearchPageResult;
import org.example.core.mysql.Where;
import org.example.core.service.impl.SimpleServiceImpl;
import org.example.meitiguanli.entity.TInformationEntity;
import org.example.meitiguanli.model.TInformationModel;
import org.example.meitiguanli.model.TInformationTagModel;
import org.example.meitiguanli.model.TTagModel;
import org.example.meitiguanli.service.TInformationService;
import org.example.meitiguanli.service.TInformationTagService;
import org.example.meitiguanli.service.TTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2024/2/16 20:44
 */
@Service
@Primary
public class TInformationServiceImpl extends SimpleServiceImpl<String, TInformationModel, TInformationEntity> implements TInformationService {

    @Autowired
    private TTagService tagService;

    @Autowired
    private TInformationTagService informationTagService;

    @Override
    public void submitInformation(Map<String, Object> params) {
        Object id = params.get("id");
        Object title = params.get("title");
        Object content = params.get("content");
        Object tags = params.get("tags");
        Object type = params.get("type");
        List<TTagModel> tagModelList = tagService.searchAll();
        Map<String, String> tagMap = tagModelList.stream().collect(Collectors.toMap(TTagModel::getName, TTagModel::getId));
        String informationId;
        if (ObjectUtils.isEmpty(id)) {
            //新增
            informationId = CommonUtils.getUUID().replaceAll("-", "");
            TInformationModel informationModel = new TInformationModel();
            informationModel.setId(informationId);
            informationModel.setContent((String) content);
            informationModel.setStatus("0");
            informationModel.setTitle((String) title);
            informationModel.setType((String) type);
            add(informationModel);
            String[] tagArray = tags.toString().split(",");
            for (String tag : tagArray) {
                //查询是否存在该标签，不存在，则新增
                TInformationTagModel informationTagModel = new TInformationTagModel();
                informationTagModel.setInformationId(informationId);
                if (!tagMap.containsKey(tag)) {
                    TTagModel tagModel = new TTagModel();
                    String tagId = CommonUtils.getUUID().replaceAll("-", "");
                    tagModel.setId(tagId);
                    tagModel.setName(tag);
                    tagService.add(tagModel);
                    informationTagModel.setTagId(tagId);
                } else {
                    informationTagModel.setTagId(tagMap.get(tag));
                }
                informationTagService.add(informationTagModel);
            }
        } else {
            informationId = (String) id;
            TInformationModel informationModel = new TInformationModel();
            informationModel.setId(informationId);
            informationModel.setContent((String) content);
            informationModel.setStatus("0");
            informationModel.setTitle((String) title);
            informationModel.setType((String) type);
            update(informationModel);
            String[] tagArray = tags.toString().split(",");
            List<TInformationTagModel> informationTagModels = informationTagService.search(Where.build().field("information_id").equal(informationId).and().field("del_flag").equal(0));
            for (TInformationTagModel informationTagModel : informationTagModels) {
                informationTagService.delete(informationTagModel.getId());
            }
            for (String tag : tagArray) {
                //查询是否存在该标签，不存在，则新增
                TInformationTagModel informationTagModel = new TInformationTagModel();
                informationTagModel.setInformationId(informationId);
                if (!tagMap.containsKey(tag)) {
                    TTagModel tagModel = new TTagModel();
                    String tagId = CommonUtils.getUUID().replaceAll("-", "");
                    tagModel.setId(tagId);
                    tagModel.setName(tag);
                    tagService.add(tagModel);
                    informationTagModel.setTagId(tagId);
                } else {
                    informationTagModel.setTagId(tagMap.get(tag));
                }
                informationTagService.add(informationTagModel);
            }
        }
    }

    @Override
    public void updateStatus(Map<String, Object> params) {
        Object id = params.get("id");
        Object status = params.get("status");
        TInformationModel informationModel = getById((String) id);
        informationModel.setStatus(status + "");
        update(informationModel);
    }

    @Override
    public SearchPageResult<JSONObject> searchList(Map<String, Object> params, int page, int limit) {
        SearchPageResult<JSONObject> result = new SearchPageResult<>();
        result.setPage(page);
        result.setLimit(limit);
        Where where = Where.build().field("1").equal(1);
        Object name = params.get("name");
        Object type = params.get("type");
        if (!ObjectUtils.isEmpty(name)) {
            where.and().field("title").like(name).or().field("content").like(name);
        }
        if (!ObjectUtils.isEmpty(type)) {
            where.and().field("type").equal(type);
        }
        result.setTotalCount(count(where));
        List<JSONObject> informationJsonArray = new ArrayList<>();
        List<TInformationModel> informationModelList = search(where.limit(page, limit));
        if (!CollectionUtils.isEmpty(informationModelList)) {
            informationModelList.stream().forEach(informationModel -> {
                JSONObject informationJsonObject = JSONUtil.parseObj(informationModel);
                informationJsonArray.add(informationJsonObject);
            });
        }
        result.setList(informationJsonArray);
        return result;
    }
}
